package com.hiwan.unions.activity.webView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.BaseWebView;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.HtmlFormat;
import com.hiwan.unions.util.Utils;
import com.yanzhenjie.nohttp.Logger;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by llg on 2016/4/11.
 */
@SuppressLint("SetJavaScriptEnabled")
public class WebViewDetailedActivity extends BaseWebView {
    private TextView textButton;
    private boolean type = true;
    private Bundle bundle;
    private String local;

    private void local(final Bundle bundle) {
        method = bundle.getString("method");
        type = bundle.getBoolean("type");
        if (!type) {
            setNavigationView();
            setStatusBarColor(R.color.white, true);
            textButton.setVisibility(View.VISIBLE);
        }
        if (bundle.getBoolean("urlType", false)) {
            myWebView.loadDataWithBaseURL("", HtmlFormat.getNewContent(bundle.getString("url")), "text/html", "UTF-8", "");
        } else {
            myWebView.loadUrl(Utils.Urls(bundle.getString("url"), userInfoC, userInfo));
        }
        getToken();
    }

    //初始GRIDVIEW数据  --离线数据
    @Override
    public void local() {
        super.local();
        if (getIntent().getExtras() != null) {
            bundle = this.getIntent().getExtras();
            local(bundle);
        }
    }

    @Override
    public void getDateMethod() {
        super.getDateMethod();
        if (bundle.getBoolean("typeUUb")) {
            if (CheckUtils.containsString(userInfoC.getString(ConstantsValues.CITYNAMEOne, ""), userInfoC.getString(ConstantsValues.CITYNAME, ""))) {
                local = userInfoC.getString(ConstantsValues.ZUOBIAO, "") + ";" + userInfoC.getString(ConstantsValues.CITYNAME, "成都市") + ";" + bundle.getString("id") + ";" + bundle.getString("typeName");
            } else {
                local = "0" + ";" + "0" + ";" + userInfoC.getString(ConstantsValues.CITYNAME, "") + ";" + bundle.getString("id") + ";" + bundle.getString("typeName");
            }
            myWebView.loadUrl("javascript:getLatAndLonAndType('" + local + "')");
            if (!type) {
                myWebView.loadUrl("javascript:getDetailById('" + bundle.getString("uuid") + "')");
            }
        }
    }

    @Override
    public void onViewCreateds() {
        super.onViewCreateds();
        setStatusBarColor(R.color.apptheme, true);
        textButton = findView(R.id.textButton);
        textButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 通过包名获取要跳转的app，创建intent对象
                Intent intent = getPackageManager().getLaunchIntentForPackage("com.hiwan.uulife");
                // 这里如果intent为空，就说名没有安装要跳转的应用嘛
                if (intent != null) {
                    try {
                        JSONObject m = new JSONObject();
                        m.put("uuid", bundle.getString("uuid"));
                        m.put("code", "uub_web1");
                        m.put("method", method);
                        m.put("url", ConstsNet.LILINTONG_UUB + "/newapp/nearby_details.html");
                        intent.putExtra("data", m.toString());
                        startActivity(intent);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    try {
                        Uri uri = Uri.parse("market://details?id=com.hiwan.uulife");
                        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(goToMarket);
                    } catch (Exception e) {
                        Uri content_url = Uri.parse("https://www.uutianfu.com/app/download.html");
                        Intent intent2 = new Intent(Intent.ACTION_VIEW, content_url);
                        startActivity(intent2);
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
